import {
  USER_LOGIN,
  USER_REGISTER,
  CLEAR_USER,
  USER_INFO,
  SAVE_BANK
} from "../constants/account";
import { setCache, getCache } from "../utils/cache";
import { CACHE_KEY } from "../service/config";

// const CACHE_KEY = "user_token";
const INITIAL_STATE = getCache(CACHE_KEY);

export default function account(state = INITIAL_STATE, action) {
  switch (action.type) {
    case USER_LOGIN:
      setCache(CACHE_KEY, {
        ...state,
        ...action
      });
      return {
        ...state,
        ...action
      };
    case USER_REGISTER:
      setCache(CACHE_KEY, {
        ...state,
        ...action
      });
      return {
        ...state,
        ...action
      };
    case USER_INFO:
      setCache(CACHE_KEY, {
        ...state,
        ...action
      });
      return {
        ...state,
        ...action
      };
    case SAVE_BANK:
      setCache(CACHE_KEY, {
        ...state,
        ...action
      });
      return {
        ...state,
        ...action
      };
    case CLEAR_USER:
      return {};
    default:
      return { ...state };
  }
}
